package cn.information.view.baseinfo.baseinfo;

import cn.information.bean.Supplier;
import cn.information.dao.SupplierDao;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.util.List;
import java.util.Vector;

public class SupplierUI extends JFrame {
    JScrollPane jScrollPane=new JScrollPane();
    AbstractTableModel tableModel;
    Vector<Vector> vector = new Vector();
    SupplierDao dao=new SupplierDao();

    JTextField txtID =new JTextField(3);
    JTextField txtName=new JTextField(10);
    JTextField txtPhone =new JTextField(10);
    JTextField txtAddress=new JTextField(10);
    JTextField txtSpname=new JTextField(10);


    public SupplierUI() {
        setTitle("供应商信息");
        setBounds(300, 300, 600, 600);
        setLayout(new BorderLayout());

        JPanel panelUp = new JPanel();
        JPanel panelCenter = new JPanel();
        JPanel panelDown = new JPanel();

        panelUp.add(jScrollPane);
        createTable();

        add(panelUp, BorderLayout.NORTH);
        add(panelCenter, BorderLayout.CENTER);
        add(panelDown, BorderLayout.SOUTH);

        panelCenter.add(new JLabel("供应商ID"));
        panelCenter.add(txtID);
        panelCenter.add(new JLabel("姓名"));
        panelCenter.add(txtName);
        panelCenter.add(new JLabel("电话"));
        panelCenter.add(txtPhone);
        panelCenter.add(new JLabel("供应产地"));
        panelCenter.add(txtAddress);
        panelCenter.add(new JLabel("商品名称"));
        panelCenter.add(txtSpname);

        JButton btnAdd = new JButton("添加");
        btnAdd.addActionListener(e -> {
            addData();
        });//lambda表达式单函数单方法

        JButton btnDelete = new JButton("删除");
        btnDelete.addActionListener(e -> {
            deleteData();
        });
        JButton btnUpdate = new JButton("修改");
        btnUpdate.addActionListener(e -> {
            upData();
        });
        JButton btnSelect = new JButton("刷新");
        btnSelect.addActionListener(e -> {
            select();
        });
        JButton btnExit = new JButton("重置");
        btnExit.addActionListener(e -> {
            txtID.setText("");
            txtName.setText("");
            txtAddress.setText("");
            txtPhone.setText("");
            txtSpname.setText("");
        });

        panelDown.add(btnAdd);
        panelDown.add(btnDelete);
        panelDown.add(btnUpdate);
        panelDown.add(btnSelect);
        panelDown.add(btnExit);

        select();
    }
    /*
     **修改数据
     */
    private void upData() {
        int id = Integer.parseInt(txtID.getText());
        Supplier supplier = new Supplier();
        supplier.setGysid(Integer.parseInt(txtID.getText()));
        supplier.setGysname(txtName.getText());
        supplier.setGysphone(txtPhone.getText());
        supplier.setAddress(txtAddress.getText());
        supplier.setSpname(txtSpname.getText());
        if (dao.updateSupplier(supplier)) {
            JOptionPane.showMessageDialog(this, "修改成功");
            select();
        } else {
            JOptionPane.showMessageDialog(this, "修改失败");
        }
    }

    /*
     * 删除数据
     * */
    private void deleteData() {
        int id = Integer.parseInt(txtID.getText());
        if(dao.deleteSupplier(id)){
            JOptionPane.showMessageDialog(this, "删除成功");
            select();
        } else {
            JOptionPane.showMessageDialog(this, "删除失败");
        }

    }

    /*添加数据**/
    private void addData() {
        Supplier supplier = new Supplier();
        supplier.setGysid(Integer.parseInt(txtID.getText()));
        supplier.setGysname(txtName.getText());
        supplier.setGysphone(txtPhone.getText());
        supplier.setAddress(txtAddress.getText());
        supplier.setSpname(txtSpname.getText());
        if(dao.addSupplier(supplier)){
            JOptionPane.showMessageDialog(this, "添加成功");
            select();
        } else {
            JOptionPane.showMessageDialog(this, "添加失败");
        }

    }

    /*创建表格*/
    private void createTable() {
        String[] columnNames = {"供应商编号", "姓名", "电话", "供应产地","商品名称"};
        JTable table = new JTable();
        jScrollPane.setViewportView(table);
        java.util.List<Supplier> list = dao.findAll();
        for (Supplier supplier : list) {
            Vector v = new Vector();
            v.add(supplier.getGysid());
            v.add(supplier.getGysname());
            v.add(supplier.getGysphone());
            v.add(supplier.getAddress());
            v.add(supplier.getSpname());
            vector.add(v);
        }
        //      List<Community>list = dao.findAll();
        tableModel = new AbstractTableModel() {
            @Override
            public int getRowCount() {
                return vector.size();
            }

            @Override
            public String getColumnName(int column) {
                return columnNames[column];
            }

            @Override
            public int getColumnCount() {
                return columnNames.length;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                if (rowIndex < vector.size()) {
                    Vector v = vector.get(rowIndex);
                    return v.get(columnIndex);
                } else {
                    return null;
                }
            }
        };
        table.setModel(tableModel);
    }
    void select(){
        vector.clear();
        List<Supplier> list = dao.findAll();
        for (Supplier supplier : list) {
            Vector v = new Vector();
            v.add(supplier.getGysid());
            v.add(supplier.getGysname());
            v.add(supplier.getGysphone());
            v.add(supplier.getAddress());
            v.add(supplier.getSpname());
            vector.add(v);
        }
        tableModel.fireTableDataChanged();
    }
}